﻿

namespace LxCFD
{
    public class PressureCorrection : PressureCorrectionBasic
    {
        public PhysicalFaceOverRelax[] PhyFaces;
        public override void CheckCreate(FvZone zone)
        {
            base.CheckCreate(zone);
            LxTool.CheckCreateArray(ref PhyFaces, Faces.Length);
        }
        protected override void set_ALU_inner(int taskIndex)
        {
            InnerIndex.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                ref FvFace face = ref Faces[i1];
                ref var mface = ref PhyFaces[i1];
                Vector3d Df_average = face.CalAverage(Dc);
                mface.SetFace(ref face, ref Df_average);
                ALUO[i1] = ALUN[i1] = -Density.GetFaceValue(i1) * mface.gDiff;
            }
        }
        protected override void set_ALU_bound(IndexNumber indexNumber, int taskIndex)
        {
            indexNumber.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                ref FvFace face = ref Faces[i1];
                ref var mface = ref PhyFaces[i1];
                Vector3d Df_average = Dc[face.OSideCell];
                mface.SetFace(ref face, ref Df_average);
                ALUO[i1] = ALUN[i1] = -Density.GetFaceValue(i1) * mface.gDiff;
            }
        }
        public override void Setb_Step1_Cor(int taskIndex)
        {
            InnerIndex.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                b_FaceCommon[i1] = Density.GetFaceValue(i1) * PhyFaces[i1].CalNonOrthogonalityCorrection_inner(ref Faces[i1], Grad, x);
            }
            foreach (var b in VelocityVector.NoSlipWalls)
            {
                Set_bFaceCommon_0(b.ThreadIndex, taskIndex);
            }
            foreach (var b in VelocityVector.Symmetries)
            {
                Set_bFaceCommon_0(b.ThreadIndex, taskIndex);
            }
            foreach (var b in VelocityVector.VelocityInlets)
            {
                Set_bFaceCommon_0(b.ThreadIndex, taskIndex);
            }
            foreach (var b in VelocityVector.PressureInOuts)
            {
                set_b_bound(b.ThreadIndex, taskIndex);
            }
        }
        protected void set_b_bound(IndexNumber indexNumber, int taskIndex)
        {
            indexNumber.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                b_FaceCommon[i1] = Density.GetFaceValue(i1) * PhyFaces[i1].CalNonOrthogonalityCorrection_bound(ref Faces[i1], Grad, x);
            }
        }
        public override void Setb_Step2_Cor(int taskIndex)
        {
            InnerIndex.CellIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                b[i1] = -MassImbalance[i1] + Cells[i1].CalIfOwnerAddElseMunus(b_FaceCommon);
            }
        }
        protected override void addPressureCorrectionToMassFlux_inner(IndexNumber indexNumber, int taskIndex)
        {
            indexNumber.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                ref FvFace face = ref Faces[i1];
                Vector3d gradPf = face.CalCFCorrectedGradient_inner(Grad, x);
                double vf = Vector3d.DotProduct(PhyFaces[i1].Normal, gradPf);// Vector3d.DotProduct(Vector3d.XXYYZZProduct(Df_average, gradPf), face.Normal);//P596
                MassFlux[i1] -= Density.GetFaceValue(i1) * vf;
            }
        }
        protected override void addPressureCorrectionToMassFlux_bound(IndexNumber indexNumber, int taskIndex)
        {
            indexNumber.FaceIDs.GetSeg(out int i1, out int endp1, taskIndex);
            for (; i1 < endp1; i1++)
            {
                ref FvFace face = ref Faces[i1];
                Vector3d gradPf = face.CalCFCorrectedGradient_bound(Grad, x);
                double vf = Vector3d.DotProduct(PhyFaces[i1].Normal, gradPf);// Vector3d.DotProduct(Vector3d.XXYYZZProduct(Df_average, gradPf), face.Normal);//P596
                MassFlux[i1] -= Density.GetFaceValue(i1) * vf;
            }
        }

    }
}
//foreach (var b in ChangablePressure)
//{
//    Set_bFaceCommon_0(b.ThreadIndex, taskIndex);
//}
//foreach (var b in FixedPressure)
//{
//    set_b_bound(b.ThreadIndex, taskIndex);
//}